//
//  TapActionViewController.m
//  礼物
//
//  Created by dllo on 15/11/10.
//  Copyright © 2015年 liangsicheng. All rights reserved.
//

#import "TapActionViewController.h"

@interface TapActionViewController ()

@end

@implementation TapActionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"美好小物";
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fanhui.png"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemAction:)];
    self.tapActionView=[[TapActionView alloc]initWithFrame:MAINSCREEN];
    [self.view addSubview:self.tapActionView];
    self.tapActionView.delegate=self;
    self.currentPage=20;
    [self GetAFNetWorkingData];
}

-(void)GetAFNetWorkingData
{
    [self addHeaderRefresh];
    [self addFooterRefresh];
}
//设置下拉刷新
-(void)addHeaderRefresh
{
    //当下拉时候会触发该方法
    self.tapActionView.myTableView.header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [AppToolss getAFNetWorkingData:@"http://api.liwushuo.com/v2/collections/22/posts?gender=1&generation=2&limit=20&offset=0" andBlock:^(id data) {
            self.tapActionView.TapActionViewCellArray=[NSMutableArray array];
            NSMutableDictionary *Dic=data;
            NSMutableArray *array=[[Dic objectForKey:@"data"]objectForKey:@"posts"];
            for (NSMutableDictionary *dic in array) {
                Model *model=[[Model alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                [self.tapActionView.TapActionViewCellArray addObject:model];
            }
            [self.tapActionView.myTableView reloadData];
            [self.tapActionView.myTableView.header endRefreshing];
        } andError:^(id failure) {
            [self.tapActionView.myTableView.header endRefreshing];
        }];
    }];
    [self.tapActionView.myTableView.header beginRefreshing];
}

-(void)addFooterRefresh
{
    //当上拉时候会触发该方法
    self.tapActionView.myTableView.footer=[MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.currentPage+=20;
        [AppToolss getAFNetWorkingData:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/111/items?ad=1&gender=1&generation=2&limit=%ld&offset=0",self.currentPage]
                              andBlock:^(id data){
                                  self.tapActionView.TapActionViewCellArray=[NSMutableArray array];
                                NSMutableDictionary *Dic=data;
                                  NSMutableArray *array=[[Dic objectForKey:@"data"]objectForKey:@"items"];
                                  for (NSMutableDictionary *dic in array) {
                                      Model *model=[[Model alloc]init];
                                      [model setValuesForKeysWithDictionary:dic];
                                      [self.tapActionView.TapActionViewCellArray addObject:model];
                                  }
                                  [self.tapActionView.myTableView reloadData];
                                  [self.tapActionView.myTableView.footer endRefreshing];
                              } andError:^(id failure) {
                                  [self.tapActionView.myTableView.footer endRefreshing];
                              }];
        
    }];
    
    
}





-(void)leftBarButtonItemAction:(UIBarButtonItem *)barButtonItem
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)tapActiononePushDetailController:(Model *)model
{
    GiftDetailViewController *detailVC=[[GiftDetailViewController alloc]init];
    detailVC.model=model;
    detailVC.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
